package com.mycjj.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.mycjj.android.R;
import com.mycjj.android.fragment.BaseFragment;
import com.mycjj.android.fragment.CarNetWorkFragment;
import com.mycjj.android.fragment.IntegralFragment;
import com.mycjj.android.fragment.MyFragment;
import com.mycjj.android.fragment.PointFragment;
import com.mycjj.android.widget.MyRadioGroup;
import com.mycjj.android.widget.MyRadioGroup.OnCheckedChangeListener;

public class MainActivity extends BaseActivity {
	private Fragment mContent;
	private RadioButton rb_menu_1,rb_menu_2,rb_menu_3,rb_menu_4;
	private MyRadioGroup rg_menu;
	private FrameLayout fl_content_frame;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		if (mContent == null){
			mContent = new MyFragment();
		}
		setContentView(R.layout.activity_main);
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit();
		
		init();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commitAllowingStateLoss();
		
	}
	
	private void init(){
		rb_menu_1 = (RadioButton)findViewById(R.id.rb_menu_1);
		rb_menu_2 = (RadioButton)findViewById(R.id.rb_menu_2);
		rb_menu_3 = (RadioButton)findViewById(R.id.rb_menu_3);
		rb_menu_4 = (RadioButton)findViewById(R.id.rb_menu_4);
		
		rg_menu = (MyRadioGroup)findViewById(R.id.rg_menu_group);
		
		fl_content_frame =(FrameLayout)findViewById(R.id.content_frame);
		
		rg_menu.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(MyRadioGroup group, int checkedId) {
				BaseFragment frag = null;
				switch (checkedId) {
					case R.id.rb_menu_1:
						frag = new MyFragment();
						break;
					case R.id.rb_menu_2:
						frag = new PointFragment();
						break;
					case R.id.rb_menu_3:
						frag = new IntegralFragment();
						break;
					case R.id.rb_menu_4:
						frag = new CarNetWorkFragment();
						break;				
				}
				if (frag != null)
					switchFragment(frag);
				else
					return;
			}
		});
		
		
	}
	private void switchFragment(Fragment fragment) {
		if (this == null)
			return;
		switchContent(fragment);
	}
	boolean isExit;  
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if (keyCode == KeyEvent.KEYCODE_BACK) {  
        	exit();
            return false;  
        } else {  
            return super.onKeyDown(keyCode, event);  
        }  
    }
	public void exit(){  
        if (!isExit) {  
            isExit = true;  
            Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();  
            mHandler.sendEmptyMessageDelayed(0, 2000);  
        } else {  
            Intent intent = new Intent(Intent.ACTION_MAIN);  
            intent.addCategory(Intent.CATEGORY_HOME);  
            startActivity(intent);  
            System.exit(0);  
        }  
    }
	
	Handler mHandler = new Handler() {  
		  
        @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  
            isExit = false;  
        }  
  
    }; 
}
